package org.adream.account.controller;

import java.util.List;
import java.util.Map;

import javax.security.auth.login.FailedLoginException;
import javax.servlet.http.HttpServletRequest;

import org.adream.account.entity.ResourceEntity;
import org.adream.account.entity.UserEntity;
import org.adream.account.service.ADreamServiceException;
import org.adream.account.service.ResourceService;
import org.adream.account.service.UserService;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.validation.Assertion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.cas.authentication.CasAuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 管理模块主页面 跳转页controller
 * @author sl
 *
 */
@Controller
public class IndexController {
	
	private final static Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private UserService userService;
	
	@Autowired
	private ResourceService resourceService;
	
	//后台管理页
	@RequestMapping(value = "/manager")
	public ModelAndView index(HttpServletRequest request) throws FailedLoginException {
		ModelAndView mv = new ModelAndView("/index");
		
		CasAuthenticationToken p = (CasAuthenticationToken)request.getUserPrincipal();
		Assertion assertion = p.getAssertion();
		AttributePrincipal principal = assertion.getPrincipal();
		Map<String, Object> map = principal.getAttributes();
		
        if(map != null) {
        	String uid = (String) map.get("uid");
        	String resIdsStr = (String)map.get("resIds");
        	String[] resIds = (resIdsStr==null?new String[0]:resIdsStr.split(","));
        	if(resIds != null && resIds.length != 0) {
        		//A类型为 account 资源
        		List<ResourceEntity> resources = resourceService.queryResourceByResIds(resIds,"A");
        		if(resources !=null && resources.size() >0) {
        			mv.getModel().put("resources", resources);
        		}
        	}
        	 
        	/*if(resIds != null && resIds.length != 0) {
        		//B类型为 task 资源
        		List<ResourceEntity> taskResources = resourceService.queryResourceByResIds(resIds,"B");
        		if(taskResources !=null && taskResources.size() >0) {
        			//只取一个
        			mv.getModel().put("taskResource", taskResources.get(0));
        		}
        	}*/
        	
			UserEntity user = userService.queryUserByUid(uid);;
			request.getSession().setAttribute("uid", uid);
			request.getSession().setAttribute("user", user);
//			mv.getModel().put("resources", resources);
        }
		return mv;
	}
	
	//欢迎页
	@RequestMapping(value = "/welcome")
	public ModelAndView welcome() {
		ModelAndView mv = new ModelAndView("/welcome");
		return mv;
	}

	//错误页
	@RequestMapping(value = "/error")
	public ModelAndView error() {
		ModelAndView mv = new ModelAndView("/error");
		return mv;
	}
	/**
	 * 敏感操作 二次登陆 入口  注意 只用在 aop拦截时
	 * @return
	 */
	@RequestMapping(value = "/aopLogin")
	public ModelAndView aopLogin() {
		ModelAndView mv = new ModelAndView("/aopLogin/aopLogin");
		return mv;
	}
}